import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class ClinicaDatos {

    public static void inicializarTabla() {
        if(!chequearTablaDoctores())
            crearTablaDoctores();
        
        if(!chequearTablaPacientes())
            crearTablaPacientes();
        
        if(!chequearTablaTurnos())
            crearTablaTurnos();
    }
	
    public static void crearTablaDoctores() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            
            boolean resultadoDoctores = stmt.execute(
                "DROP TABLE IF EXISTS doctores;" +
                "CREATE TABLE doctores (" +
                    "dni int primary key, " +
                    "nombre varchar(255)," +
                    "apellido varchar(255)," +
                    "especializacion varchar(255)," +
                    ");"
            );
            
            
            System.out.println("Creando tabla doctores");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void crearTablaPacientes() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            
            boolean resultadoPacientes = stmt.execute(
                "DROP TABLE IF EXISTS pacientes;" +
                "CREATE TABLE pacientes (" +
                    "dni int primary key, " +
                    "nombre varchar(255)," +
                    "apellido varchar(255)," +
                    "edad int," +
                    "fecNac varchar(255)" +
                    ");"
            );
            
            
            System.out.println("Creando tabla pacientes");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void crearTablaTurnos() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            
            boolean resultadoTurnos = stmt.execute(
                "DROP TABLE IF EXISTS turnos;" +
                "CREATE TABLE turnos (" +
                    "idTurno int primary key, " +
                    "dniPaciente int," +
                    "dniDoctor int," +
                    "fecha date," +
                    "hora varchar(255)," +
                    ");"
            );
            
            
            System.out.println("Creando tabla turnos");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    
    public static boolean chequearTablaDoctores() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            boolean res = stmt.execute(
                "SELECT * FROM doctores LIMIT 1;"
            );
            System.out.println("La tabla doctores ya existe");
            return true;
        } catch (SQLException ex) {
            System.out.println("La tabla doctores NO existe");
            return false;
        }
    }
	
    public static boolean chequearTablaPacientes() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            boolean res = stmt.execute(
                "SELECT * FROM pacientes LIMIT 1;"
            );
            System.out.println("La tabla pacientes ya existe");
            return true;
        } catch (SQLException ex) {
            System.out.println("La tabla pacientes NO existe");
            return false;
        }
    }
	
    public static boolean chequearTablaTurnos() {
        try {
            Connection con = DriverManager.getConnection("jdbc:h2:~/clinica");
            
            Statement stmt = con.createStatement();
            boolean res = stmt.execute(
                "SELECT * FROM turnos LIMIT 1;"
            );
            System.out.println("La tabla turnos ya existe");
            return true;
        } catch (SQLException ex) {
            System.out.println("La tabla turnos NO existe");
            return false;
        }
    }

}

